﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JustTank
{
    class JustTankMain
    {
        const int WorldRows = 35;
        const int WorldCols = 50;
        const int TankLength = 6;

        static void Initialize(Engine engine)
        {
            int startRow = 3;
            int startCol = 7;
            int endRow = WorldRows - 1;
            int endCol = WorldCols - 2;

            Console.SetWindowSize(70, 40);
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth = Console.WindowWidth;
            
            //for (int i = startCol; i < endCol; i++)
            //{
            //    Block currBlock = new Block(new MatrixCoords(startRow + 1, i));

            //    engine.AddObject(currBlock);
            //}

            //Inputing map sides
            for (int i = startRow; i < WorldRows; i++)
            {
                IndestructibleBlock indestrBlockLeft = new IndestructibleBlock(new MatrixCoords(i, startCol - 1), '|');
                IndestructibleBlock indestrBlockLeftTwo = new IndestructibleBlock(new MatrixCoords(i, startCol - 2), '|');
                IndestructibleBlock indestrBlockRighr = new IndestructibleBlock(new MatrixCoords(i, endCol + 1), '|');
                IndestructibleBlock indestrBlockRighrTwo = new IndestructibleBlock(new MatrixCoords(i, endCol), '|');

                engine.AddObject(indestrBlockLeft);
                engine.AddObject(indestrBlockRighr);
                engine.AddObject(indestrBlockLeftTwo);
                engine.AddObject(indestrBlockRighrTwo);
            }

            //Inputing map side
            for (int i = startCol - 2; i < endCol + 2; i++)
            {
                IndestructibleBlock indestrBlockCieling = new IndestructibleBlock(new MatrixCoords(startRow, i), '-');
                IndestructibleBlock indestrBlockCielingTwo = new IndestructibleBlock(new MatrixCoords(startRow - 1, i), '-');

                engine.AddObject(indestrBlockCieling);
                engine.AddObject(indestrBlockCielingTwo);
            }
  
            //Inputing map blocks
            for (int i = startRow + 3 ; i < WorldRows - 22; i++)
            {
                IndestructibleBlock indestrBlockLeft = new IndestructibleBlock(new MatrixCoords(i, startCol + 2 ), '|');                

                engine.AddObject(indestrBlockLeft);                
            }
            for (int i = startRow + 3; i < WorldRows - 22; i++)
            {
                IndestructibleBlock indestrBlockLeft = new IndestructibleBlock(new MatrixCoords(i, startCol + 3), '|');

                engine.AddObject(indestrBlockLeft);
            }
            for (int i = startRow + 3; i < WorldRows - 22; i++)
            {
                IndestructibleBlock indestrBlockLeft = new IndestructibleBlock(new MatrixCoords(i, startCol + 35), '|');

                engine.AddObject(indestrBlockLeft);
            }
            for (int i = startRow + 3; i < WorldRows - 22; i++)
            {
                IndestructibleBlock indestrBlockLeft = new IndestructibleBlock(new MatrixCoords(i, startCol + 36), '|');

                engine.AddObject(indestrBlockLeft);
            }
            for (int i = startRow + 10; i < WorldRows - 14; i++)
            {
                IndestructibleBlock indestrBlockRight = new IndestructibleBlock(new MatrixCoords(i, startCol + 15), '|');                

                engine.AddObject(indestrBlockRight);      
            }
            for (int i = startRow + 10; i < WorldRows - 14; i++)
            {
                IndestructibleBlock indestrBlockRight = new IndestructibleBlock(new MatrixCoords(i, startCol + 16), '|');

                engine.AddObject(indestrBlockRight);
            }
            for (int i = startRow + 21; i < WorldRows - 4; i++)
            {
                IndestructibleBlock indestrBlockRight = new IndestructibleBlock(new MatrixCoords(i, startCol + 15), '|');

                engine.AddObject(indestrBlockRight);
            }
            for (int i = startRow + 21; i < WorldRows - 4; i++)
            {
                IndestructibleBlock indestrBlockRight = new IndestructibleBlock(new MatrixCoords(i, startCol + 16), '|');

                engine.AddObject(indestrBlockRight);
            }
            for (int i = startRow + 10; i < WorldRows - 7; i++)
            {
                IndestructibleBlock indestrBlockRight = new IndestructibleBlock(new MatrixCoords(i, startCol + 24), '|');

                engine.AddObject(indestrBlockRight);
            }
            for (int i = startRow + 10; i < WorldRows - 7; i++)
            {
                IndestructibleBlock indestrBlockRight = new IndestructibleBlock(new MatrixCoords(i, startCol + 23), '|');

                engine.AddObject(indestrBlockRight);
            }
            for (int i = startCol + 2; i < WorldCols - 25; i++)
            {
                IndestructibleBlock indestrBlockRightHorisontal = new IndestructibleBlock(new MatrixCoords(startRow + 3, i), '-');

                engine.AddObject(indestrBlockRightHorisontal);        
                
            }
            for (int i = startCol + 2; i < WorldCols - 25; i++)
            {
                IndestructibleBlock indestrBlockRightHorisontal = new IndestructibleBlock(new MatrixCoords(startRow + 4, i), '-');

                engine.AddObject(indestrBlockRightHorisontal);

            }

            for (int i = startCol + 8; i < WorldCols - 27; i++)
            {
                IndestructibleBlock indestrBlockRightHorisontal = new IndestructibleBlock(new MatrixCoords(startRow + 9, i), '-');

                engine.AddObject(indestrBlockRightHorisontal);

            }
            for (int i = startCol + 8; i < WorldCols - 27; i++)
            {
                IndestructibleBlock indestrBlockRightHorisontal = new IndestructibleBlock(new MatrixCoords(startRow + 10, i), '-');

                engine.AddObject(indestrBlockRightHorisontal);

            }
            for (int i = startCol + 23; i < WorldCols - 15; i++)
            {
                IndestructibleBlock indestrBlockRightHorisontal = new IndestructibleBlock(new MatrixCoords(startRow + 10, i), '-');

                engine.AddObject(indestrBlockRightHorisontal);

            }
            for (int i = startCol + 23; i < WorldCols - 15; i++)
            {
                IndestructibleBlock indestrBlockRightHorisontal = new IndestructibleBlock(new MatrixCoords(startRow + 9, i), '-');

                engine.AddObject(indestrBlockRightHorisontal);

            }


            //Inputing map side
            for (int i = startCol - 2; i < endCol + 2; i++)
            {
                IndestructibleBlock indestrBlockBottom = new IndestructibleBlock(new MatrixCoords(endRow, i),'-');
                IndestructibleBlock indestrBlockBottomTwo = new IndestructibleBlock(new MatrixCoords(endRow - 1, i), '-');

                engine.AddObject(indestrBlockBottom);
                engine.AddObject(indestrBlockBottomTwo);
            }

            TankHuman theTank = new TankHuman(new MatrixCoords(10 , 10));
            
            engine.AddObject(theTank);

            TankBot bot = new TankBot(new MatrixCoords(15, 15));
            TankBot bot2 = new TankBot(new MatrixCoords(7, 10));
            TankBot bot3 = new TankBot(new MatrixCoords(6, 22));
            engine.AddObject(bot);
            engine.AddObject(bot2);
            engine.AddObject(bot3);
        }
        static private void PrintStartMenu()
        {
            string label = "Start Game with Tanks:";
            Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2);
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write(label);

            label = "W - MOVE UP";
            Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 1);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(label);

            label = "A - MOVE LEFT";
            Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 2);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(label);

            label = "D - MOVE RIGHT";
            Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 3);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(label);

            label = "S - MOVE DOWN";
            Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 4);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(label);

            label = "SPACE - SHOOT";
            Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 5);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(label);

            label = "Press 1 to start:";
            Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2 + 6);
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(label);
            while (true) //running the game
            {


                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo menuChoice = Console.ReadKey();
                    if (menuChoice.Key == ConsoleKey.D1)
                    {
                        break;
                    }
                }
            }
            Console.Clear();
        
        }

        static void PrintGameOver()
        {
            Console.Clear();
            string label = "GAME OVER";
            Console.SetCursorPosition((Console.WindowWidth / 2) - (label.Length / 2), (Console.WindowHeight - 2) / 2);
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write(label);
            Console.SetCursorPosition(Console.WindowWidth - 2, Console.WindowHeight - 2);


        }

        static void Main(string[] args)
        {

            IRenderer renderer = new ConsoleRenderer(WorldRows, WorldCols);
            IUserInterface keyboard = new KeyboardInterface();

            Engine gameEngine = new Engine(renderer, keyboard, 0);

            keyboard.OnLeftPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerTankLeft();
            };

            keyboard.OnRightPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerTankRight();
            };

            keyboard.OnUpPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerTankUp();
            };

            keyboard.OnDownPressed += (sender, eventInfo) =>
            {
                gameEngine.MovePlayerTankDown();
            };

            keyboard.OnActionPressed += (sender, eventInfo) =>
            {
                gameEngine.ShootPlayrTank();
            };

            Initialize(gameEngine);

            PrintStartMenu();

            gameEngine.Run();

            PrintGameOver();
        }
    }
}
